package me.numark;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.utils.Utils;

/**
 * Created by tian on 14-9-20:下午10:20.
 */
public class LogInActivity extends AuthenticatorActivity implements OnClickListener {

    @InjectView(R.id.mobile_edit)
    EditText mobileEdit;

    @InjectView(R.id.password_edit)
    EditText passwordEdit;

    @InjectView(R.id.forget_password)
    TextView forgetPassword;

    @InjectView(R.id.log_in_button)
    Button logInButton;

    @InjectView(R.id.register_button)
    Button registerButton;

    @InjectView(R.id.qq_log_in_button)
    ImageView qqLogInButton;

    @InjectView(R.id.weibo_log_in_button)
    ImageView weiboLogInButton;

    @InjectView(R.id.weixin_log_in_button)
    ImageView weixinLogInButton;

    private boolean DEBUG = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        ButterKnife.inject(this);
        mobileEdit = (EditText) findViewById(R.id.mobile_edit);
        passwordEdit = (EditText) findViewById(R.id.password_edit);
        if (DEBUG) {
            mobileEdit.setText("13910028267");
            passwordEdit.setText("tian1234");
        }
        forgetPassword.setOnClickListener(this);
        logInButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
        weiboLogInButton.setOnClickListener(this);
        qqLogInButton.setOnClickListener(this);
        weixinLogInButton.setOnClickListener(this);
    }

    private boolean isInputValid() {
        return !TextUtils.isEmpty(mobileEdit.getText().toString()) && !TextUtils
                .isEmpty(passwordEdit.getText().toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.log_in_button:
                if (isInputValid()) {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("username", mobileEdit.getText().toString());
                    params.put("password", passwordEdit.getText().toString());
                    params.put("type", "1");
                    doLogIn(params);
                    showProgress(R.string.logging_in);
                }
                break;
            case R.id.weibo_log_in_button:
                mSsoHandler.authorize(this);
                break;
            case R.id.qq_log_in_button:
                if (mTencent == null) {
                    Utils.showToast(R.string.qq_not_installed);
                    return;
                }
                mTencent.login(this, "all", this);
                break;
            case R.id.weixin_log_in_button:
                if (mWxApi.isWXAppInstalled() && mWxApi.isWXAppSupportAPI()) {
                } else {
                    Utils.showToast(R.string.weixin_oauth_failed);
                }
                break;
            case R.id.register_button:
                startActivityForResult(new Intent(this, RegisterActivity.class), 0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case 0:
                //TODO
                break;
        }
    }
}
